import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import liu.de.dto.Bean;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

import java.time.LocalDateTime;

import static liu.de.config.RabbitMqConfig.QUEUE_TEST;

/**
 * @author liude
 */
public class Send {
    public static void main(String[] args) {
        RabbitTemplate rabbitTemplate = rabbitTemplate();


        Bean data = new Bean("a", LocalDateTime.now());
        rabbitTemplate.convertAndSend(QUEUE_TEST, data);


        ((CachingConnectionFactory) rabbitTemplate.getConnectionFactory()).destroy();
    }


    private static RabbitTemplate rabbitTemplate() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses("localhost:5672");

        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);

        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(objectMapper()));

        return rabbitTemplate;
    }


    private static ObjectMapper objectMapper() {
        return new ObjectMapper()
                .findAndRegisterModules()
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                ;
    }
}
